<template>
  <van-form @submit="onSubmit">
    <van-cell-group inset>
      <van-field
          v-model="userAccount"
          name="用户名"
          label="用户名"
          placeholder="用户名"
          :rules="[{ required: true, message: '请填写用户名' }]"
      />
      <van-field
          v-model="userPassword"
          type="password"
          name="密码"
          label="密码"
          placeholder="密码"
          :rules="[{ required: true, message: '请填写密码' }]"
      />
      <router-link
          to="/user/register"
          style="font-size: 10px; color: #417AB2;position: fixed;right: 30px;"
      >
        没有账号？点击去注册
      </router-link>
    </van-cell-group>
    <div style="margin: 20px; ">
      <van-button round block type="primary" native-type="submit">
        登录
      </van-button>
    </div>
  </van-form>
</template>

<script setup>
import {ref} from "vue";
import myAxios from "../plugins/myAxios";
import {showFailToast, showSuccessToast} from "vant";
import {useRouter} from "vue-router";
import {getCurrentUser} from "../services/user.js";

const router = useRouter();

const userAccount = ref('');
const userPassword = ref('');
const checkPassword = ref('');

const onSubmit = async () => {
  const res = await myAxios.post("/user/login", {
    userAccount: userAccount.value,
    userPassword: userPassword.value
  })
  console.log(res)
  if (res.code === 0 && res.data){
    getCurrentUser();
    showSuccessToast('登陆成功')
    router.replace({
      path: '/'
    })
  }else {
    showFailToast('登陆失败');

  }
};
</script>

<style scoped>

</style>